//
//  FeedBackBottomView.m
//  SoupApp
//
//  Created by pro1 on 2018/11/20.
//  Copyright © 2018 pro2. All rights reserved.
//

#import "FeedBackBottomView.h"

@interface FeedBackBottomView ()<QMUITextViewDelegate>

@property (strong,nonatomic) UIButton *keyBordBtn;
@property (strong,nonatomic) UIButton *sendBtn;
@property (strong,nonatomic) QMUITextView *textView;
@property (nonatomic, assign) CGFloat textViewMinimumHeight;
@property (assign,nonatomic) NSInteger maxLength;

@end

@implementation FeedBackBottomView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = UIColorMakeWithHex(@"#F5F5F5");
        [self addSubview:self.keyBordBtn];
        [self addSubview:self.sendBtn];
        _maxLength = 500;
        _textViewMinimumHeight = 33;
        self.textView = [[QMUITextView alloc] init];
        self.textView.delegate = self;
        self.textView.placeholder = @"请输入你的意见和建议";
        self.textView.layer.cornerRadius = 4.0;
        self.textView.clipsToBounds = YES;
        self.textView.font = [UIFont fontWithName:@"PingFangSC-Medium" size:14];
        self.textView.placeholderColor = UIColorPlaceholder; // 自定义 placeholder 的颜色
        self.textView.textContainerInset = UIEdgeInsetsMake(6.5, 16.5, 6.5, 16.5);
        self.textView.typingAttributes = @{NSFontAttributeName: UIFontRegularMake(14),
                                           NSForegroundColorAttributeName: UIColor.grayColor,
                                           NSParagraphStyleAttributeName: [NSMutableParagraphStyle qmui_paragraphStyleWithLineHeight:20]};
        self.textView.textAlignment = NSTextAlignmentCenter;
        // 限制可输入的字符长度
        self.textView.maximumTextLength = 500;
        self.textView.maximumTextLength = _maxLength;
        // 限制输入框自增高的最大高度
        self.textView.backgroundColor = [UIColor whiteColor];
        self.textView.maximumHeight = 130;
        [self addSubview:self.textView];
        self.textView.frame = CGRectMake(20, 7, self.frame.size.width - 112, _textViewMinimumHeight);
    }
    return self;
}

- (UIButton *)keyBordBtn{
    if (!_keyBordBtn) {
        _keyBordBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.frame.size.width - 44 - 34, 0, 34, 47)];
        _keyBordBtn.imageView.contentMode = UIViewContentModeScaleAspectFit;
        [_keyBordBtn setImage:UIImageMake(@"emjo") forState:UIControlStateNormal];
        _keyBordBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 10);
    }
    return _keyBordBtn;
}

- (UIButton *)sendBtn{
    if (!_sendBtn) {
        _sendBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.frame.size.width - 44, 0, 44, 47)];
        _sendBtn.imageView.contentMode = UIViewContentModeScaleAspectFit;
        [_sendBtn setImage:UIImageMake(@"发送") forState:UIControlStateNormal];
        _sendBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 20);
        [_sendBtn addTarget:self action:@selector(sendBtnPressed) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sendBtn;
}

#pragma mark - <QMUITextViewDelegate>

- (void)textViewDidEndEditing:(UITextView *)textView{
    if (!textView.text ||textView.text.length == 0) {
        textView.textAlignment = NSTextAlignmentCenter;
    }
    else{
        textView.textAlignment = NSTextAlignmentLeft;
    }
    [self bottomFrameChanged:self.frame.size.height];
}

// 实现这个 delegate 方法就可以实现自增高
- (void)textView:(QMUITextView *)textView newHeightAfterTextChanged:(CGFloat)height {
    if (!textView.text ||textView.text.length == 0) {
        textView.textAlignment = NSTextAlignmentCenter;
    }
    else{
        textView.textAlignment = NSTextAlignmentLeft;
    }
    height = fmax(height, self.textViewMinimumHeight);
    textView.frame = CGRectMake(20, 7, textView.bounds.size.width, height);
    CGFloat newHeight = height + 14 + PreferredValueForNotchedDevice(39, 0);
    CGFloat oldHeight = self.bounds.size.height;
    [self bottomFrameChanged:newHeight];
    [UIView animateWithDuration:0.2 animations:^{
        self.bounds = CGRectMake(0, 0, self.bounds.size.width, newHeight);
        self.center = CGPointMake(self.center.x, self.center.y - (newHeight - oldHeight)/2);
    }];
}


- (void)textViewDidChange:(UITextView *)textView{
    NSInteger maxFontNum = _maxLength;//最大输入限制
    NSString *toBeString = textView.text;
    // 获取键盘输入模
    NSString *lang = [[UIApplication sharedApplication] textInputMode].primaryLanguage;
    if ([lang isEqualToString:@"zh-Hans"]) { // zh-Hans代表简体中文输入，包括简体拼音，健体五笔，简体手写
        UITextRange *selectedRange = [textView markedTextRange];
        //获取高亮部分
        UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
        // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
        if (!position) {
            if (toBeString.length > maxFontNum) {
                textView.text = [toBeString substringToIndex:maxFontNum];//超出限制则截取最大限制的文本
            } else {
                
            }
        }
    } else {// 中文输入法以外的直接统计
        if (toBeString.length > maxFontNum) {
            textView.text = [toBeString substringToIndex:maxFontNum];
            
        } else {
            
        }
    }
}


- (void)textView:(QMUITextView *)textView didPreventTextChangeInRange:(NSRange)range replacementText:(NSString *)replacementText {
    [QMUITips showWithText:[NSString stringWithFormat:@"文字不能超过 %@ 个字符", @(textView.maximumTextLength)] inView:self hideAfterDelay:2.0];
}

// 可以利用这个 delegate 来监听发送按钮的事件，当然，如果你习惯以前的方式的话，也可以继续在 textView:shouldChangeTextInRange:replacementText: 里处理
- (BOOL)textViewShouldReturn:(QMUITextView *)textView {
    //    [QMUITips showSucceed:[NSString stringWithFormat:@"成功发送文字：%@", textView.text] inView:self.view hideAfterDelay:3.0];
    //    textView.text = nil;
    // return YES 表示这次 return 按钮的点击是为了触发“发送”，而不是为了输入一个换行符
    return NO;
}

- (void)bottomFrameChanged:(CGFloat)height{
    if (_delegate) {
        [_delegate bottomFrameChanged:height];
    }
}

- (void)sendBtnPressed{
    [_textView resignFirstResponder];
    if (!_textView.text || _textView.text.length == 0) {
        [QMUITips showInfo:@"请输入你的意见和建议" inView:self.superview hideAfterDelay:2.0];
        return;
    }
    if (_delegate) {
        [self userSendTextWith:_textView.text];
    }
}

- (void)userSendTextWith:(NSString *)text{
    [_delegate userSendTextWith:text];
}
- (void)reloadBootomView{
    CGFloat newHeight =(PreferredValueForNotchedDevice(39, 0) + 47);
    [self bottomFrameChanged:newHeight];
    
    [UIView animateWithDuration:0.2 animations:^{
        self.bounds = CGRectMake(0, 0, self.bounds.size.width, newHeight);
        self.center = CGPointMake(self.center.x, SCREEN_HEIGHT - newHeight/2);
    }];
    _textView.text = nil;
}




@end
